package com.yfbao.horizon.inspection.prometheus;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class PrometheusResponse {
    private String status;
    private ResponseData data;
    private String errorType;
    private String error;

    @Data
    public static class ResponseData {
        private String resultType;
        private List<Result> result;
    }

    @Data
    public static class Result {
        private Map<String, String> metric;
        private List<Object> value; // 瞬时向量 [timestamp, value]
        private List<List<Object>> values; // 范围向量 [[timestamp, value], ...]

        /**
         * 获取指标值（瞬时向量）
         */
        public Double getInstantValue() {
            if (value != null && value.size() == 2) {
                return Double.parseDouble(value.get(1).toString());
            }
            return null;
        }

        /**
         * 获取时间序列数据（范围向量）
         */
        public List<TimeSeriesPoint> getTimeSeries() {
            if (values == null) return Collections.emptyList();

            return values.stream()
                    .map(point -> new TimeSeriesPoint(
                            Long.parseLong(point.get(0).toString()),
                            Double.parseDouble(point.get(1).toString())
                    ))
                    .collect(Collectors.toList());
        }
    }

    @Data
    @AllArgsConstructor
    public static class TimeSeriesPoint {
        private long timestamp;
        private double value;
    }
}
